<?php


namespace app\api\controller\assistant;

use addons\epay\library\Service;
use app\api\controller\xyb\Base;
use app\api\service\DistributorService;
use app\api\service\DynamicService;
use app\api\service\UsersService;

use app\common\model\distributor\Activity as ActivityModel;
use EasyWeChat\Factory;


class Distributor extends Base
{
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /*首页；活动信息*/
    public function user_participation()
    {

        $users = $this->getAuthUsers();

        $user_circle = UsersService::formatUsersCircleStatusInfo($users->id, $users->circle_id);

        $res = DistributorService::retrieveUsersOngoing($users->id, $user_circle['circle_id']);

        $this->success('success', $res);
    }

    /*生成二维码*/
    public function create_qrcode()
    {
        $users = $this->getAuthUsers();
        $config = Service::getConfig('wechat');
        $app = Factory::miniProgram([
            "app_id" => $config['miniapp_id'],
            "secret" => $config['miniapp_secret'],
            'response_type' => 'array',
        ]);
        $response = $app->app_code->get('pagesE/news/receive_handle?user_id=' . $users->id, [
            'width' => 100, 'env_version' => 'trial',
        ]);
        $dest_dir = ROOT_PATH . 'public/assistant/distributor/';
        $filename = '';
        $url = '';
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $filename = $response->saveAs($dest_dir, $users->id . '.png');
            $url = '/assistant/distributor/' . $filename;
        }
        $res = DistributorService::retrieveActivityInfoOngoing();
        $this->success('success', [
            'url'           => cdnurl($url),
            'self_reward'   => isset($res['self_reward'])?$res['self_reward']:[],
            'inviter_reward'=> isset($res['inviter_reward'])?$res['inviter_reward']:[],
            'share_img'     => isset($res['share_img'])?cdnurl($res['share_img']):'',
            'share_title'   => isset($res['share_title'])?$res['share_title']:'',
            'sub_title'     => isset($res['sub_title'])?$res['sub_title']:'',
            'title'         => isset($res['banner'])?$res['banner']:'',
            'banner'         => isset($res['banner'])?$res['banner']:'',
        ]);
    }

    /*用户开始激活，生成助力进度表*/
    public function user_registration_participate()
    {
        $users = $this->getAuthUsers();

        $res = DistributorService::userRegistrationParticipate($users->id);
        if( $res['code'] == 0){
            $this->error($res['msg'], '');
        }
        $this->success($res['msg'], $res['data']);
    }

    /*被邀请人开始助力；显示助力详情*/
    public function invite_people_assist_details()
    {
//        if(!$users_id = $this->request->post('users_id')){
//            $this->error('参数错误');
//        }
        $res = DistributorService::retrieveActivityInfoOngoing();

        $this->success('获取成功', $res);
    }

    /*被邀请人开始助力；邀请人*/
    public function invite_people_assist()
    {
        $users = $this->getAuthUsers();
        if(!$progress_id = $this->request->post('progress_id')){
            $this->error('参数错误');
        }
        $user_circle = UsersService::formatUsersCircleStatusInfo($users->id, $users->circle_id);
        $res = DistributorService::invitePeopleAssist($users->id, $progress_id, $user_circle['circle_id']);
        if( $res['code'] == 0){
            $this->error($res['msg'], (isset($res['data'])?$res['data']:[]));
        }
        $this->success($res['msg'], (isset($res['data'])?$res['data']:[]));
    }

    /*助力列表*/
    public function help_list()
    {
        $users = $this->getAuthUsers();
        $status = $this->request->post('status', 0);
        $type = $this->request->post('type', 1);
        $res = DistributorService::helpList($users->id, $status, $type);
        $this->success('success', $res);
    }


}